package com.bjpowernode.springboot.web;

import com.bjpowernode.springboot.model.User;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @RequestMapping(value = "/login")
    @ResponseBody
    public Object login(HttpServletRequest request){
        //将用户的信息存放到session中
        User user = new User();
        user.setId(1001);
        user.setName("sun");
        request.getSession().setAttribute("user",user);
        return "login SUCCESS";

    }

    //用户登录之后才可以访问
    @RequestMapping(value = "/center")
    @ResponseBody
    public Object center(){
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
                System.out.println(i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "see Center Message";
    }

    //该请求不登录也可以访问
    @RequestMapping(value = "/out")
    @ResponseBody
    public Object out(){
        return "Out see anytime";
    }

    //如果用户未登录，并访问了需要登录才可以访问的请求，会跳转至该目录
    @RequestMapping(value = "/error")
    @ResponseBody
    public Object error(){

        return "error";
    }
}
